﻿using System;
using System.Web.Mail;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using MLY;
using MLY.Dal;
using MLY.Bll;
using System.IO;

namespace MLY.Reports.ChartReports
{
    public partial class DataFlowReport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // direction
                if (SessParam.Culture == "he")
                {
                    //cssLiteral.Text = @"<link type=""text/css"" rel=""stylesheet"" href=""/Style/Rtl_Stylesheet.css"" />";
                    DirectionPanel.Direction = ContentDirection.RightToLeft;
                }
                else
                {
                    //cssLiteral.Text = @"<link type=""text/css"" rel=""stylesheet"" href=""/Style/Ltr_Stylesheet.css"" />";
                    DirectionPanel.Direction = ContentDirection.LeftToRight;
                }

                // client/contractor
                if (Request.QueryString["userRole"] != null)
                {
                    UserRoleHiddenField.Value = Request.QueryString["userRole"].ToString();
                }
                else
                {
                    UserRoleHiddenField.Value = "Client";
                }

                if (IsDemo())
                {
                    DemoPanel1.Visible = true;
                    DemoPanel2.Visible = true;
                    DemoPanel3.Visible = true;
                    DemoPanel4.Visible = true;
                    txtFromDate.Text = "01/01/2010";
                    txtToDate.Text = "01/01/2013";
                }
                else
                {
                    DemoPanel1.Visible = false;
                    DemoPanel2.Visible = false;
                    DemoPanel3.Visible = false;
                    DemoPanel4.Visible = false; 
                }
            }
        }

        private bool IsDemo()
        {
            if (Session["UserId"].ToString() == "39")
                return true;
            return false;
        }

        protected void ddlOrgs_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ddlOrgs.Items.Clear();
                ListItem all = new ListItem("All", "0");
                ddlOrgs.Items.Add(all);
                ddlOrgs.AppendDataBoundItems = true;
                if (UserRoleHiddenField.Value == "Client")
                {
                    ddlOrgs.DataSource = MLY.Bll.clsOrganization.GetContractorsByClientId((int)Session["OrganizationId"]);
                    ddlOrgs.DataBind();
                }
                else
                {
                    ddlOrgs.DataSource = MLY.Bll.clsOrganization.GetClientsByContractorId((int)Session["OrganizationId"]);
                    ddlOrgs.DataBind();
                }
            }
        }

        protected void GenerateReport(object sender, EventArgs e)
        {
            int jump = 5;
            int minInterval = 0;
            int maxInterval = 20;            
            
            DateTime PopulationFrom = DateTime.Parse(txtFromDate.Text.Trim());
            DateTime PopulationTill = DateTime.Parse(txtToDate.Text.Trim());

            string DateType = DateTypeDDL.SelectedValue;

            int CustomerId;
            int ContractorId;

            if (UserRoleHiddenField.Value == "Client")
            {
                CustomerId = (int)Session["OrganizationId"];
                ContractorId = int.Parse(ddlOrgs.SelectedValue);
            }
            else
            {
                ContractorId = (int)Session["OrganizationId"];
                CustomerId = int.Parse(ddlOrgs.SelectedValue);
            }

            using (Dal.lqKbmDataContext db = new MLY.Dal.lqKbmDataContext())
            {
                List<WorkOrdersView> lst = (from v in db.WorkOrdersViews
                                            where (CustomerId == 0 || CustomerId == v.CustomerId) &&
                                                  (ContractorId == 0 || ContractorId == v.ContractorId) &&
                                                  ((DateType == "1" && v.SendDate >= PopulationFrom 
                                                                    && v.SendDate <= PopulationTill) ||
                                                   (DateType == "2" && v.RequestedDelivaryDate >= PopulationFrom 
                                                                    && v.RequestedDelivaryDate <= PopulationTill) || 
                                                   (DateType == "3" && v.PlannedDelivaryDate >= PopulationFrom
                                                                    && v.PlannedDelivaryDate <= PopulationTill) || 
                                                   (DateType == "4" && v.ActualDelivaryDate >= PopulationFrom
                                                                    && v.ActualDelivaryDate <= PopulationTill))  &&
                                                  ((!v.ActualDelivaryDate.HasValue) ||(v.ActualDelivaryDate.Value < DateTime.Now))
                                            select v).ToList();
                int currentMin = minInterval;
                int currentMax = minInterval + jump;
                int index = 1;
                int value;
                while (currentMin <= maxInterval)
                {
                    string PointTitle = (currentMax > maxInterval) ? currentMin.ToString() + "+" : currentMin.ToString() + " - " + currentMax.ToString();

                    value = CountWorkOrders(currentMin, currentMax, maxInterval, lst);

                    DataFlowChart.Series[0].Points.AddXY(index, (double)value);
                    System.Web.UI.DataVisualization.Charting.CustomLabel CustomLabel =
                        new System.Web.UI.DataVisualization.Charting.CustomLabel
                        {
                            Text = PointTitle,
                            FromPosition = index - 0.5,
                            ToPosition = index + 0.5
                        };
                    DataFlowChart.ChartAreas["ChartArea1"].AxisX.CustomLabels.Add(CustomLabel);
                    
                    currentMin = currentMax;
                    currentMax += jump;       
                    index ++;
                }
            }

            if (IsDemo())
            {
                DemoPanel1.Visible = false;
                DemoPanel2.Visible = false;
                DemoPanel3.Visible = false;
                DemoPanel4.Visible = false;
                DemoChartPanel.Visible = true;
            }

            DataFlowChart.Visible = true;
        }

        private int CountWorkOrders(int currentMin, int currentMax, int maxInterval, List<WorkOrdersView> lst)
        {
            int Count = 0;

            DateTime CurDate = DateTime.Now;
            
            string FromDate = FromDateDDL.SelectedValue;
            string TillDate = TillDateDDL.SelectedValue;
            /*
              SendDate 1
              RequestedDelivaryDate 2
              PlannedDelivaryDate 3                      
              ActualDelivaryDate 4
             */
            if (FromDate == TillDate)
                Count = 0;
            else if (FromDate == "1" && TillDate == "2")
            { 
                Count = lst.Where(l => (MLY.Utils.TranslateNormalDaysToWorkDays(((l.RequestedDelivaryDate.HasValue ? l.RequestedDelivaryDate.Value : CurDate) - 
                                                                               (l.SendDate.HasValue ? l.SendDate.Value : CurDate)).Days) >= currentMin) && 
                                      ((currentMax > maxInterval) ||
                                       (MLY.Utils.TranslateNormalDaysToWorkDays(((l.RequestedDelivaryDate.HasValue ? l.RequestedDelivaryDate.Value : CurDate) -
                                                                               (l.SendDate.HasValue ? l.SendDate.Value : CurDate)).Days) <= currentMax ))).Count();
            }
            else if (FromDate == "1" && TillDate == "3")
            {
                Count = lst.Where(l => (MLY.Utils.TranslateNormalDaysToWorkDays(((l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate) -
                                                                               (l.SendDate.HasValue ? l.SendDate.Value : CurDate)).Days) >= currentMin) &&
                                      ((currentMax > maxInterval) ||
                                       (MLY.Utils.TranslateNormalDaysToWorkDays(((l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate) -
                                                                               (l.SendDate.HasValue ? l.SendDate.Value : CurDate)).Days) <= currentMax))).Count();
            }
            else if (FromDate == "1" && TillDate == "4")
            {
                Count = lst.Where(l => (MLY.Utils.TranslateNormalDaysToWorkDays(((l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate) -
                                                                               (l.SendDate.HasValue ? l.SendDate.Value : CurDate)).Days) >= currentMin) &&
                                      ((currentMax > maxInterval) ||
                                       (MLY.Utils.TranslateNormalDaysToWorkDays(((l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate) -
                                                                               (l.SendDate.HasValue ? l.SendDate.Value : CurDate)).Days) <= currentMax))).Count();
            }
            else if (FromDate == "2" && TillDate == "1")
            {
                Count = lst.Where(l => (MLY.Utils.TranslateNormalDaysToWorkDays(((l.SendDate.HasValue ? l.SendDate.Value : CurDate) -
                                                                               (l.RequestedDelivaryDate.HasValue ? l.RequestedDelivaryDate.Value : CurDate)).Days) >= currentMin) &&
                                      ((currentMax > maxInterval) ||
                                       (MLY.Utils.TranslateNormalDaysToWorkDays(((l.SendDate.HasValue ? l.SendDate.Value : CurDate) -
                                                                               (l.RequestedDelivaryDate.HasValue ? l.RequestedDelivaryDate.Value : CurDate)).Days) <= currentMax))).Count();
            }
            else if (FromDate == "2" && TillDate == "3")
            {
                Count = lst.Where(l => (MLY.Utils.TranslateNormalDaysToWorkDays(((l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate) -
                                                                               (l.RequestedDelivaryDate.HasValue ? l.RequestedDelivaryDate.Value : CurDate)).Days) >= currentMin) &&
                                      ((currentMax > maxInterval) ||
                                       (MLY.Utils.TranslateNormalDaysToWorkDays(((l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate) -
                                                                               (l.RequestedDelivaryDate.HasValue ? l.RequestedDelivaryDate.Value : CurDate)).Days) <= currentMax))).Count();
            }
            else if (FromDate == "2" && TillDate == "4")
            {
                Count = lst.Where(l => (MLY.Utils.TranslateNormalDaysToWorkDays(((l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate) -
                                                                               (l.RequestedDelivaryDate.HasValue ? l.RequestedDelivaryDate.Value : CurDate)).Days) >= currentMin) &&
                                      ((currentMax > maxInterval) ||
                                       (MLY.Utils.TranslateNormalDaysToWorkDays(((l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate) -
                                                                               (l.RequestedDelivaryDate.HasValue ? l.RequestedDelivaryDate.Value : CurDate)).Days) <= currentMax))).Count();
            }
            else if (FromDate == "3" && TillDate == "1")
            {
                Count = lst.Where(l => (MLY.Utils.TranslateNormalDaysToWorkDays(((l.SendDate.HasValue ? l.SendDate.Value : CurDate) -
                                                                               (l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate)).Days) >= currentMin) &&
                                      ((currentMax > maxInterval) ||
                                       (MLY.Utils.TranslateNormalDaysToWorkDays(((l.SendDate.HasValue ? l.SendDate.Value : CurDate) -
                                                                               (l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate)).Days) <= currentMax))).Count();
            }
            else if (FromDate == "3" && TillDate == "2")
            {
                Count = lst.Where(l => (MLY.Utils.TranslateNormalDaysToWorkDays(((l.RequestedDelivaryDate.HasValue ? l.RequestedDelivaryDate.Value : CurDate) -
                                                                               (l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate)).Days) >= currentMin) &&
                                      ((currentMax > maxInterval) ||
                                       (MLY.Utils.TranslateNormalDaysToWorkDays(((l.RequestedDelivaryDate.HasValue ? l.RequestedDelivaryDate.Value : CurDate) -
                                                                               (l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate)).Days) <= currentMax))).Count();
            }
            else if (FromDate == "3" && TillDate == "4")
            {
                Count = lst.Where(l => (MLY.Utils.TranslateNormalDaysToWorkDays(((l.RequestedDelivaryDate.HasValue ? l.RequestedDelivaryDate.Value : CurDate) -
                                                                               (l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate)).Days) >= currentMin) &&
                                      ((currentMax > maxInterval) ||
                                       (MLY.Utils.TranslateNormalDaysToWorkDays(((l.RequestedDelivaryDate.HasValue ? l.RequestedDelivaryDate.Value : CurDate) -
                                                                               (l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate)).Days) <= currentMax))).Count();
            }
            else if (FromDate == "4" && TillDate == "1")
            {
                Count = lst.Where(l => (MLY.Utils.TranslateNormalDaysToWorkDays(((l.SendDate.HasValue ? l.SendDate.Value : CurDate) -
                                                                               (l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate)).Days) >= currentMin) &&
                                      ((currentMax > maxInterval) ||
                                       (MLY.Utils.TranslateNormalDaysToWorkDays(((l.SendDate.HasValue ? l.SendDate.Value : CurDate) -
                                                                               (l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate)).Days) <= currentMax))).Count();
            }

            else if (FromDate == "4" && TillDate == "2")
            {
                Count = lst.Where(l => (MLY.Utils.TranslateNormalDaysToWorkDays(((l.RequestedDelivaryDate.HasValue ? l.RequestedDelivaryDate.Value : CurDate) -
                                                                               (l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate)).Days) >= currentMin) &&
                                      ((currentMax > maxInterval) ||
                                       (MLY.Utils.TranslateNormalDaysToWorkDays(((l.RequestedDelivaryDate.HasValue ? l.RequestedDelivaryDate.Value : CurDate) -
                                                                               (l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate)).Days) <= currentMax))).Count();
            }
            else if (FromDate == "4" && TillDate == "3")
            {
                Count = lst.Where(l => (MLY.Utils.TranslateNormalDaysToWorkDays(((l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate) -
                                                                               (l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate)).Days) >= currentMin) &&
                                      ((currentMax > maxInterval) ||
                                       (MLY.Utils.TranslateNormalDaysToWorkDays(((l.PlannedDelivaryDate.HasValue ? l.PlannedDelivaryDate.Value : CurDate) -
                                                                               (l.ActualDelivaryDate.HasValue ? l.ActualDelivaryDate.Value : CurDate)).Days) <= currentMax))).Count();
            }
            return Count;
        }
    }
}
